<template>
  <div class="d_main">
    <!-- 操作区 S-->
    <div class="d_operationBox">
      <div class="d_btn">

      </div>
      <div class="d_search">
      <screec
          v-model="search.QueryDataBaseFiled"
          :headData.sync="tableHead"
          :disabled="searchLoading"
        ></screec>
        <el-input
          v-model="search.likeWords"
          placeholder="请输入关键字"
          size="small"
          @keyup.enter.native="getScrapList"
        >
          <el-button
            slot="append"
            :loading="searchLoading"
            type="primary"
            icon="el-icon-search"
            :disabled="searchLoading"
            element-loading-spinner="el-icon-loading"
            @click="getScrapList"
          >
            搜索
          </el-button>
        </el-input>
      </div>
    </div>
    <!-- 操作区 E-->
    <!-- 表格操作区 S-->
    <div class="d_tableOperation">
      <div class="d_condition">

      </div>
      <div class="d_set">
        <div @click="Refresh"><i class="d_icon el-icon-refresh-right" /></div>
        <div class="setting flex">
          <HeadSeting :list-data.sync="tableHead" :table-code="$config.semifinishedproductllbilllist" @reset="getTableHead" />
        </div>
      </div>
    </div>
    <!-- 表格操作区 E-->
    <!--table板块-->
    <div ref="tableCon" class="tableCon">
      <el-table
        v-show="tableHead.length>0"
        id="emtable"
        ref="multipleTable"
        v-loading="searchLoading"
        :data="dataLists"
        :height="tableHeight || 'auto'"
        border
        fit
        style="width: 100%"
        @header-dragend="surverWidth"
      >
        <template v-for="(fruit,index) in tableHead">
          <el-table-column
            v-if="fruit.visible"
            :key="index"
            :label="fruit.columnName"
            :min-width="fruit.columnWidth || 110"
            :prop="fruit.columnCode"
            show-overflow-tooltip
            :fixed="fruit.isFloat?'left':false"
          >
            <template slot-scope="scope">
              <template v-if="fruit.columnCode==='outStockState'">
                <div class="flex">
                  <template v-if="scope.row.outStockState===0">
                    <span class="mr5 spot" style="background: #3F61F1" /><span>待确认</span>
                  </template>
                  <template v-else-if="scope.row.outStockState===1">
                    <span class="mr5 spot" style="background: #67C23A" /><span>已确认</span>
                  </template>
                  <template v-else>
                    <span class="mr5 spot" style="background:#E13335" /><span>已拒绝</span>
                  </template>
                </div>
              </template>
              <template v-else>
                {{ scope.row[fruit.columnCode] }}
              </template>
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" width="70px" fixed="right" align="center">
          <template slot-scope="scope">
            <div>
              <el-popover
                :ref="`popover-${scope.$index}`"
                placement="bottom-end"
                popper-class="tableOp"
                width="160"
                :popper-options="{ boundariesElement: 'opBox', removeOnDestroy: true }"
                trigger="click"
              >
                <div class="operation">
                  <ul>
                    <li @click="seeInfo(scope.row)">查看</li>
                  </ul>
                </div>
                <div slot="reference" class="opBox pointer"><span class="el-icon-more c_3F61F1" /></div>
              </el-popover>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 骨架屏     -->
      <div v-if="tableHead.length==0" class="p16">
        <el-skeleton :rows="6" animated />
      </div>
    </div>
    <!-- 分页 S-->
    <div class="d_pag">
      <pagination
        :total="total||0"
        :page.sync="search.pageIndex"
        :limit.sync="search.pageSize"
        @pagination="getScrapList"
      />
    </div>
    <!-- 分页 E-->
    <!--  查看报废出库弹框   -->
    <el-dialog
      title="查看报废出库"
      :visible.sync="isSeevisible"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"><span class="mr5">出库单号：</span>{{ outForm.outStockBillNo || '系统自动生成' }}</span>
          <span class="mr15"><span class="mr5">创建日期：</span>{{ outForm.createTime || dayjs().format('YYYY-MM-DD') }}</span>
          <span><span class="mr5">创建人员：</span>{{ outForm.createPersonName }}</span>
        </div>
        <div class="addWuliaoBox">
          <el-form
            ref="outForm"
            :model="outForm"
            label-width="95px"
            disabled
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="出库类型">
                  <el-input v-model="outForm.outStockTypeName" placeholder="请输入" size="mini" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="关联单据">
                  <el-input v-model="outForm.scrapBillNo" placeholder="关联单据" size="mini" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="申请人员">
                  <el-input
                    v-model="outForm.applyPersonName"
                    placeholder="请输入内容"
                    size="mini"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="报废备注" class="inlineText" prop="outStockRemark">
                  <el-input v-model="outForm.outStockRemark" type="textarea" :rows="1" placeholder="请输入退货备注" size="mini" clearable />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">报废明细</span>
            </div>
            <el-table
              ref="multipleTable"
              :data="outForm.mxDataList"
              border
              fit
              style="width: 100%"
              :expand-row-keys="expends"
              :row-key="getRowKeys"
            >
              <el-table-column type="expand">
                <template slot-scope="{row}">
                  <div class="dialogExpandTable">
                    <el-table
                      :data="row.productPackageList || []"
                      :header-cell-style="{background:'#3f61f110'}"
                    >
                      <el-table-column
                        prop="cartonSizeName"
                        label="纸箱规格"
                        min-width="90"
                      />
                      <el-table-column
                        prop="packageDescription"
                        label="包装描述"
                        min-width="90"
                      />
                      <el-table-column
                        prop="eachBoxQuantity"
                        label="每箱数量"
                        min-width="90"
                      />
                      <el-table-column
                        prop="stockBoxCount"
                        label="库存箱数"
                        min-width="90"
                      />
                      <el-table-column
                        prop="stockQuantity"
                        label="库存数量"
                        min-width="90"
                      >
                        <template slot-scope="scope">
                          <div class="flex">
                            <template>
                              <span class="mr5" style="color: #E13335">{{ scope.row.stockQuantity }}</span>
                            </template>
                          </div>
                        </template>
                      </el-table-column>

                      <el-table-column
                        label="报废箱数"
                        prop="outStockBoxAmount"
                        show-overflow-tooltip
                        min-width="90"
                      />
                      <el-table-column
                        label="报废数量"
                        prop="outStockQuantity"
                        show-overflow-tooltip
                        min-width="90"
                      />
                      <el-table-column
                        label="备注"
                        prop="outStockMxRemark"
                        show-overflow-tooltip
                        min-width="90"
                      />
                    </el-table>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                prop="productNo"
                label="成品编号"
                width="120"
              />
              <el-table-column
                prop="productTypeName"
                label="成品类型"
                width="120"
              />
              <el-table-column
                prop="productName"
                label="产品名称"
                width="auto"
              />
              <el-table-column
                prop="printingSurface"
                label="板面"
                width="120"
              />
              <el-table-column
                prop="productVersion"
                label="版本"
                width="120"
              />
              <el-table-column
                prop="sumBoxCount"
                label="库存箱数"
                width="120"
              />
              <el-table-column
                prop="sumStockQuantity"
                label="库存数量"
                width="120"
              >
                <template slot-scope="scope">
                  <div class="flex">
                    <template>
                      <span class="mr5" style="color: #E13335">{{ scope.row.sumStockQuantity }}</span>
                    </template>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                prop="productUnit"
                label="单位"
                width="60"
              />
            </el-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isSeevisible=false">取 消</el-button>
        <!-- 确定       -->
        <el-button
          type="primary"
          size="mini"
          @click="isSeevisible=false"
        >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { arraySort } from '@/utils'
import Pagination from '@/components/Pagination'
import HeadSeting from '@/components/HeadSeting'
import { mapGetters } from 'vuex'
import screec from "@/components/screen";
export default {
  name: '',
  components: { Pagination, HeadSeting,screec },
  computed: {
    ...mapGetters([
      'name',
      'userInfo'
    ])
  },
  data() {
    return {
      isSeevisible: false, // 查看弹框是否打开
      infoLoading: false,
      searchLoading: false, // 列表加载效果
      search: {
         QueryDataBaseFiled:'',
        pageIndex: 1,
        pageSize: 30,
        outStockType: 2, // 出库类型
        likeWords: ''
      },
      total: 0,
      dataLists: [],
      tableHead: [], // 表头
      tableHeight: null,
      expends: [],
      outForm: {
      }
    }
  },
  created() {
    // 报废出库表头
    this.getTableHead()
  },
  mounted() {
    this.calHeight()
  },
  methods: {
    getRowKeys(row) {
      return row.productNo
    },
    getExpends() {
      var productNo = this.outForm.mxDataList.map(e => e.productNo)
      this.expends = productNo
    },
    // 查看领料出库
    seeInfo(data) {
      this.isSeevisible = true
      this.ops = 'see'
      this.getInfo(data.outStockBillID)
    },
    // 领料单详情
    getInfo(id) {
      const params = {
        outStockBillID: id
      }
      this.infoLoading = true
      this.$api.warehouse.GetCPScrapOutStockBillDetail(params).then(res => {
        this.outForm = res.baseData
        this.outForm.mxDataList = res.mxDataList
        this.infoLoading = false
      }).catch(() => {
        this.infoLoading = false
      })
    },
    // 报废出库列表，清除所有查询条件
    Refresh() {
      this.search = this.$options.data().search
      this.getScrapList()
    },
    // 报废出库表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.cpscrapoutstocklist
      }
      this.$api.common.getAllTableHead(parmas).then(res => {
        res.sort(arraySort('columnSort'))
        this.tableHead = res
        // 报废出库列表
        this.getScrapList()
      })
    },
    // 报废出库列表
    getScrapList() {
      const params = this.search
      this.searchLoading = true
      this.$api.warehouse.GetMatchPageCPScrapOutStockBillDataList(params).then(res => {
        this.dataLists = res.list
        this.dataList.forEach((item)=>{
            for(let i in item){
              if(!item[i] && item[i] !== 0){
                item[i] = '--'
              }
            }
          })
        this.total = res.totalCount
        this.searchLoading = false
      }).catch(() => {
        this.searchLoading = false
      })
    },
    // 保存表头样式变化
    saveHead(newArray) {
      const parmas = newArray
      setTimeout(() => {
        this.$api.common.saveTableStyle(parmas).then(() => {
        })
      }, 300)
    },
    // 保存表头列宽度
    surverWidth(newWidth, oldWidth, column) {
      setTimeout(() => {
        const heads = [...this.tableHead]
        heads.forEach(item => {
          if (item.columnCode === column.property) {
            item.columnWidth = column.width
            return
          }
        })
        this.saveHead(heads)
      }, 100)
    },
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.tableCon.getBoundingClientRect()
        this.tableHeight = rect.height
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.spot{
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 100%;
}
.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}
</style>
